package com.daoImpl;

import java.sql.Timestamp;
import java.util.List;

import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.bean.CraftGrade;
import com.dao.CraftGradeDao;

/**
 * A data access object (DAO) providing persistence and search support for
 * CraftGrade entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.bean.CraftGrade
 * @author MyEclipse Persistence Tools
 */
public class CraftGradeDaoImpl extends HibernateDaoSupport implements CraftGradeDao {
	private static final Logger log = LoggerFactory
			.getLogger(CraftGradeDaoImpl.class);
	// property constants
	public static final String GRADE_NAME = "gradeName";
	public static final String WAGE = "wage";
	public static final String UNIT = "unit";

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftGradeDao#save(com.bean.CraftGrade)
	 */
	@Override
	public void save(CraftGrade transientInstance) {
		log.debug("saving CraftGrade instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftGradeDao#delete(com.bean.CraftGrade)
	 */
	@Override
	public void delete(CraftGrade persistentInstance) {
		log.debug("deleting CraftGrade instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftGradeDao#findById(java.lang.Integer)
	 */
	@Override
	public CraftGrade findById(java.lang.Integer id) {
		log.debug("getting CraftGrade instance with id: " + id);
		try {
			CraftGrade instance = (CraftGrade) getHibernateTemplate().get(
					"com.bean.CraftGrade", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftGradeDao#findByExample(com.bean.CraftGrade)
	 */
	@Override
	public List findByExample(CraftGrade instance) {
		log.debug("finding CraftGrade instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftGradeDao#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding CraftGrade instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from CraftGrade as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftGradeDao#findByGradeName(java.lang.Object)
	 */
	@Override
	public List findByGradeName(Object gradeName) {
		return findByProperty(GRADE_NAME, gradeName);
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftGradeDao#findByWage(java.lang.Object)
	 */
	@Override
	public List findByWage(Object wage) {
		return findByProperty(WAGE, wage);
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftGradeDao#findByUnit(java.lang.Object)
	 */
	@Override
	public List findByUnit(Object unit) {
		return findByProperty(UNIT, unit);
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftGradeDao#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all CraftGrade instances");
		try {
			String queryString = "from CraftGrade";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftGradeDao#merge(com.bean.CraftGrade)
	 */
	@Override
	public CraftGrade merge(CraftGrade detachedInstance) {
		log.debug("merging CraftGrade instance");
		try {
			CraftGrade result = (CraftGrade) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftGradeDao#attachDirty(com.bean.CraftGrade)
	 */
	@Override
	public void attachDirty(CraftGrade instance) {
		log.debug("attaching dirty CraftGrade instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftGradeDao#attachClean(com.bean.CraftGrade)
	 */
	@Override
	public void attachClean(CraftGrade instance) {
		log.debug("attaching clean CraftGrade instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static CraftGradeDao getFromApplicationContext(ApplicationContext ctx) {
		return (CraftGradeDao) ctx.getBean("CraftGradeDaoImpl");
	}
}